/**
 * 游戏通用弹窗
 */
class AlertUI extends UIBase{
    private img_tittle:eui.Image;
    private lb_tips:eui.Label;
    // private img_icon:eui.Image;
    // private lb_award:eui.Label;
    private btn_next:BaseButton;
    private btn_sure:BaseButton;

    private btn_close:BaseButton;
    private lb_normal_tips:eui.Label;
    private award_1:eui.Group;
    private award_2:eui.Group;

    private img_icon_1:eui.Image;
    private lb_award_1:eui.Label;
    private img_icon_2:eui.Image;
    private lb_award_2:eui.Label;

    private lb_tittle:eui.Label;
    
    constructor(data?)
    {
        super('AlertView',data)
    }

    public init()
    {
        super.init(true);
        let type = this.data.type;
        if(type == alertType.nextCustoms || type == alertType.special || type == alertType.task)
        {
            //下一关
            this.btn_next.visible = true;
            let gold = this.data.gold;
            let diamond = this.data.diamond;
            this.lb_tips.text = '恭喜获得收益';
            this.lb_tittle.text = '收益翻倍';
            this.lb_tittle.visible = true;
            this.img_tittle.visible = false;
            // this.lb_award.text = 
            // this.img_icon.source = 'gold_icon_png';
            if(gold)
            {
                if(type == alertType.nextCustoms)
                this.lb_award_1.text = `x${ 2 * gold}`;
                else
                this.lb_award_1.text = `x${gold}`;
                this.img_icon_1.source = 'icon_gold';
                RoleCache.instance().myMoney += parseInt(gold);
            }
            if(diamond)
            {
                this.award_1.left = 0;
                this.award_2.visible = true;
                if(type == alertType.nextCustoms)
                this.lb_award_2.text = `x${2 * diamond}`;
                else
                this.lb_award_2.text = `x${diamond}`;
                
                this.img_icon_2.source = 'icon_diamond';
                RoleCache.instance().myDiamond += parseInt(diamond);
            }
            // SoundManager.instance().playSound('win_mp3', 1);
        }
        else
        {
            this.btn_sure.visible = true;
            switch(type)
            {
                case alertType.threeShare:
                    this.initThreeUI();
                    break;
                case alertType.killDragon:
                    this.initDragonUI();
                    break;
                case alertType.default:
                    this.initTipsUI();
                    break;
            }
        }
    }

    protected initTipsUI()
    {
        this.lb_tittle.visible = true;
        this.img_tittle.visible = false;
        this.lb_tittle.text = '提示';
        this.lb_tips.text = this.data.tips;
        let award:string = this.data.award;
        if(award)
        {
            let awardArr = award.split(',');
            awardArr.forEach((value,index,array)=>{
                let info = ResourceUtil.instance().parseSignleAward(value);
                let icon = this['img_icon_'+(index+1)] as eui.Image;
                icon.visible = true;
                icon.source = info.itemIcon;
                let label = this['lb_award_'+(index+1)] as eui.Label;
                label.visible = true;
                label.text = 'x'+info.itemNum;
            })
        }
    }

    protected initThreeUI()
    {
        this.lb_normal_tips.visible = true;
        this.img_tittle.source = 'tittle_three_png';
        this.lb_tips.text = '获得三箭齐发x5';
        this.img_icon_1.source = 'icon_three_png';
        this.lb_award_1.text = `x5`;
    }

    protected initDragonUI()
    {
        this.lb_normal_tips.visible = true;
        this.img_tittle.source = 'tittle_kill_png';
        this.lb_tips.text = '获得屠龙宝刀x2';
        this.img_icon_1.source = 'icon_dao_png';
        this.lb_award_1.text = `x2`;
        this.lb_normal_tips.text = '宝刀一出，谁与争锋'
    }

    public addEvent()
    {
        this.btn_next.addTapHandler(this.close, this);
        this.btn_sure.addTapHandler(this.sureHandle, this);
        this.btn_close.addTapHandler(this.close, this);
    }

    protected close()
    {
        let type = this.data.type;
        if(type == alertType.nextCustoms)
        {
            // let logic:GameUI = UIManager.instance().getLogicByName(UIConst.GAME_UI) as GameUI;
            // if(logic)
            // {
            //     logic.nextCustoms(true);
            // }
            let acLogic:AccountUI = UIManager.instance().getLogicByName(UIConst.ACCOUNT_UI) as AccountUI;
            if(acLogic)
            {
                acLogic.close();
            }
            // GameUtil.dispatchEvent(EventConst.RESOURCE_CHANGE);
            this.remove(0);
            return;
        }
        else if(type == alertType.special)
        {
            UIManager.instance().closeUILogic(UIConst.AWARDCUSTOMS_UI);
            GameUtil.dispatchEvent(EventConst.RESOURCE_CHANGE);
        }
        else if(type == alertType.task)
        {
            UIManager.instance().closeUILogic(UIConst.TASK_UI);
            GameUtil.dispatchEvent(EventConst.RESOURCE_CHANGE);
        }
        this.remove(4);
    }

    protected sureHandle()
    {
        let type = this.data.type;
        switch(type)
        {
            case alertType.threeShare:
                SharePrase.instance().requestOneAdvertisement({type:ShareId.ThreeKnife});
                break;
            case alertType.killDragon:
                // this.showDragon();
                SharePrase.instance().requestOneAdvertisement({type:ShareId.killDragon});
                break;
            case alertType.default:
                this.close();
                break;
        }
    }

    public showDragon()
    {
        let self = this;
        let Success_call = function()
        {  
            ResourceUtil.instance().addResource('a_3_'+GameConst.GameConst.kill_dragon_count);
            self.close();
            // this.initDragonTimes();
            let logic:GameUI = UIManager.instance().getLogicByName(UIConst.GAME_UI) as GameUI;
            if(logic)
            logic.updateDragonGroup(true);
            GameUtil.sendCntTOSever('video_num');
        };
        let Error_call = function()
        {  
            MessageUtil.tips('观看完整视频才能拥有屠龙宝刀哦！');
        };
        WeChatgGameManager.instance().showVideoAd(GameConst.adList.other_vedio, Success_call, Error_call, this);
    }

    public removeEvent()
    {
        this.btn_close.removeTapHandler();
        this.btn_next.removeTapHandler();
        this.btn_sure.removeTapHandler();
    }

    public clear()
    {
        super.clear();
    }
}

enum alertType{
    nextCustoms, //下一关
	killDragon,  //屠龙
    threeShare, //三发
    special, //特殊奖励关卡结束
    default, //默认
    task,   //任务关卡结束
    Max
}